/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/09/16
 * Program: SWTReservationDisplay.java
 */
package vn.smartware.co.client.business;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import vn.smartware.co.commons.Const;
import vn.smartware.co.commons.SWTUtils;
import vn.smartware.co.framework.common.BaseConstant;
import vn.smartware.co.framework.exception.FatalException;
import vn.smartware.co.framework.exception.SessionTimeOutException;
import vn.smartware.co.framework.plugin.LangResources;
import vn.smartware.co.framework.swt.base.JNetComposite;
import vn.smartware.co.framework.utils.MsgBoxUtils;
import vn.smartware.co.java.net.commons.Utils;

/**
 * SWTReservationDisplay
 * @author thuctt
 */
public class SWTReservationDisplay extends JNetComposite {

	private static Log log = LogFactory.getLog(SWTReservationDisplay.class);

	private Text txtTel;

	private Text txtIdNumber;

	private Text txtCusName;

	private Combo cmbRentStatus;

	private Combo cmbRentType;

	private Text txtFinishYmd;

	private Text txtBeginYmd;
	
	private Button btnReserveInput;

	private Button btnClear;

	private Table tblInfo;

	private Button btnSearch;

	private Text txtRoom;

	private Button btnClose;

	private Group grpSearch;

	private CLabel clblMessage;
	
	private static final int TBL_ROW_COUNT = 20;
	
	/**
	 * Constructor.
	 * @param parent 
	 * @param style 
	 * @param prgName 
	 */
	public SWTReservationDisplay(Composite parent, int style, String prgName) {

		super(parent, style, prgName);

		try {
//			setBackground(BaseConstant.COLOR_BGR_BASE);
//			
//			executeInit();
			
			if (!initData()){
				fatalErrorMsgBox();
				return;
			}
			
			clearAction();

		} catch (Exception e) {
			log.error("Exception.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * init data to display
	 * @throws FatalException
	 */
	@SuppressWarnings("unchecked")
	private boolean initData() throws FatalException {

//		try {
			Map map = new HashMap();
			map.put(Const.SYSTEM_CONFIG.RENT_TYPE, Boolean.TRUE);
			map.put(Const.SYSTEM_CONFIG.RESERVE_STATUS, Boolean.TRUE);
//			map = SWTUtils.ComboProcess.getComboInitItems(map, this);
			
			String[] items = (String[])map.get(Const.SYSTEM_CONFIG.RENT_TYPE);
			if (items == null || items.length == 0 || Utils.isNullEmpty(items[1])){
				MsgBoxUtils.errorMsgBox("errors.COMBO_DATA_NOT_SET", 
						new Object[]{LangResources.getText("SWTReservationDisplay.lblRentType")});
				return false;
			}
			cmbRentType.setItems(items);
			//
			items = (String[])map.get(Const.SYSTEM_CONFIG.RESERVE_STATUS);
			if (items == null || items.length == 0 || Utils.isNullEmpty(items[1])){
				MsgBoxUtils.errorMsgBox("errors.COMBO_DATA_NOT_SET", 
						new Object[]{LangResources.getText("SWTReservationDisplay.lblRentStatus")});
				return false;
			}
			cmbRentStatus.setItems(items);
		
//		} catch (SessionTimeOutException e) {
//			log.warn("SessionTimeOutException.", e);
//		}
		return true;
	}

	/**
	 * init controls
	 */
	protected void initialize() {

		grpSearch = new Group(this, SWT.NONE);
		grpSearch.setSize(782, 170);
		grpSearch.setLocation(15, 15);
		grpSearch.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		final CLabel lblRoom = new CLabel(grpSearch, SWT.CENTER | SWT.BORDER);
		lblRoom.setBounds(30, 25, 100, 22);
		lblRoom.setFont(BaseConstant.FONT_LINK_LABEL);
		lblRoom.setForeground(BaseConstant.COLOR_LINK_LABEL);
		lblRoom.setText(LangResources.getText("SWTReservationDisplay.lblRoom"));

		txtRoom = new Text(grpSearch, SWT.BORDER);
		txtRoom.setBounds(136, 25, 132, 22);
		txtRoom.setTextLimit(10);
		txtRoom.setFont(BaseConstant.FONT_BASE_TEXT);
		txtRoom.setBackground(BaseConstant.COLOR_TEXT_NORMAL); 

		final CLabel lblRentType = new CLabel(grpSearch, SWT.CENTER | SWT.BORDER);
		lblRentType.setBounds(30, 53, 100, 22);
		lblRentType.setFont(BaseConstant.FONT_BASE_LABEL);
		lblRentType.setText(LangResources.getText("SWTReservationDisplay.lblRentType"));

		cmbRentType = new Combo(grpSearch, SWT.READ_ONLY);
		cmbRentType.setBounds(136, 53, 132, 24);
		cmbRentType.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		cmbRentType.setBackground(BaseConstant.COLOR_TEXT_NORMAL);

		final CLabel lblRentStatus = new CLabel(grpSearch, SWT.CENTER | SWT.BORDER);
		lblRentStatus.setBounds(30, 81, 100, 22);
		lblRentStatus.setFont(BaseConstant.FONT_BASE_LABEL);
		lblRentStatus.setText(LangResources.getText("SWTReservationDisplay.lblRentStatus"));

		cmbRentStatus = new Combo(grpSearch, SWT.READ_ONLY);
		cmbRentStatus.setBounds(136, 81, 132, 24);
		cmbRentStatus.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		cmbRentStatus.setBackground(BaseConstant.COLOR_TEXT_NORMAL);

		final CLabel lblReserveYmd = new CLabel(grpSearch, SWT.CENTER | SWT.BORDER);
		lblReserveYmd.setBounds(30, 109, 100, 22);
		lblReserveYmd.setFont(BaseConstant.FONT_BASE_LABEL);
		lblReserveYmd.setText(LangResources.getText("SWTReservationDisplay.lblReserveYmd"));

		txtBeginYmd = new Text(grpSearch, SWT.BORDER);
		txtBeginYmd.setTextLimit(10);
		txtBeginYmd.setBounds(136, 109, 100, 22);
		txtBeginYmd.setFont(BaseConstant.FONT_BASE_TEXT);
		txtBeginYmd.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		txtBeginYmd.addFocusListener(new FocusAdapter() {
			public void focusLost(final FocusEvent arg0) {
				txtBeginYmd.setText(SWTUtils.OperatorUI.formatYYYYMMDD(txtBeginYmd.getText()));
			}
		});
		
		final Label lbl1 = new Label(grpSearch, SWT.CENTER);
		lbl1.setText("�");
		lbl1.setBounds(242, 113, 22, 18);
		lbl1.setFont(BaseConstant.FONT_BASE_LABEL);
		lbl1.setBackground(BaseConstant.COLOR_BGR_BASE);

		txtFinishYmd = new Text(grpSearch, SWT.BORDER);
		txtFinishYmd.setTextLimit(10);
		txtFinishYmd.setBounds(271, 109, 100, 22);
		txtFinishYmd.setFont(BaseConstant.FONT_BASE_TEXT);
		txtFinishYmd.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		txtFinishYmd.addFocusListener(new FocusAdapter() {
			public void focusLost(final FocusEvent arg0) {
				txtFinishYmd.setText(SWTUtils.OperatorUI.formatYYYYMMDD(txtFinishYmd.getText()));
			}
		});

		final Group grpCustomer = new Group(grpSearch, SWT.NONE);
		grpCustomer.setBounds(404, 20, 349, 111);
		grpCustomer.setText(LangResources.getText("SWTReservationDisplay.grpCustomer"));
		grpCustomer.setBackground(BaseConstant.COLOR_BGR_BASE);

		final CLabel lblCustomer = new CLabel(grpCustomer, SWT.CENTER | SWT.BORDER);
		lblCustomer.setForeground(BaseConstant.COLOR_LINK_LABEL);
		lblCustomer.setFont(BaseConstant.FONT_LINK_LABEL);
		lblCustomer.setBounds(10, 23, 100, 22);
		lblCustomer.setText(LangResources.getText("SWTReservationDisplay.lblCustomer"));

		txtCusName = new Text(grpCustomer, SWT.BORDER);
		txtCusName.setFont(BaseConstant.FONT_BASE_TEXT);
		txtCusName.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		txtCusName.setBounds(116, 23, 223, 22);
		txtCusName.setTextLimit(50);

		final CLabel lblIdNumber = new CLabel(grpCustomer, SWT.CENTER | SWT.BORDER);
		lblIdNumber.setFont(BaseConstant.FONT_BASE_LABEL);
		lblIdNumber.setBounds(10, 51, 100, 22);
		lblIdNumber.setText(LangResources.getText("SWTReservationDisplay.lblIdNumber"));

		txtIdNumber = new Text(grpCustomer, SWT.BORDER);
		txtIdNumber.setFont(BaseConstant.FONT_BASE_TEXT);
		txtIdNumber.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		txtIdNumber.setBounds(116, 51, 100, 22);
		txtIdNumber.setTextLimit(15);

		final CLabel lblTel = new CLabel(grpCustomer, SWT.CENTER | SWT.BORDER);
		lblTel.setFont(BaseConstant.FONT_BASE_LABEL);
		lblTel.setBounds(10, 79, 100, 22);
		lblTel.setText(LangResources.getText("SWTReservationDisplay.lblTel"));

		txtTel = new Text(grpCustomer, SWT.BORDER);
		txtTel.setFont(BaseConstant.FONT_BASE_TEXT);
		txtTel.setBackground(BaseConstant.COLOR_TEXT_NORMAL);
		txtTel.setBounds(116, 79, 100, 22);
		txtTel.setTextLimit(15);
		
		btnSearch = new Button(grpSearch, SWT.NONE);
		btnSearch.setText(LangResources.getText("btnSearch"));
		btnSearch.setBounds(653, 137, 100, 23);
		btnSearch.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnSearch.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnSearch.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clblMessage.setText("");
				searchAction();
			}
		});
		
		final Label line1 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		line1.setBounds(0, 191, 812, 15);
		line1.setBackground(BaseConstant.COLOR_BGR_BASE);

		tblInfo = new Table(this, SWT.FULL_SELECTION | SWT.BORDER);
		tblInfo.addSelectionListener(new SelectionAdapter() {
			public void widgetDefaultSelected(final SelectionEvent arg0) {
				clblMessage.setText("");
				
			}
		});
		tblInfo.setLinesVisible(true);
		tblInfo.setHeaderVisible(true);
		tblInfo.setBounds(15, 212, 782, 397);
		tblInfo.setFont(BaseConstant.FONT_BASE_LABEL);
		tblInfo.setData(BaseConstant.TABLE_ROW_COUNT, Integer.toString(TBL_ROW_COUNT));

		final TableColumn tcolHidden = new TableColumn(tblInfo, SWT.NONE);
		tcolHidden.setWidth(0);
		
		final TableColumn tcolReserveNo = new TableColumn(tblInfo, SWT.NONE);
		tcolReserveNo.setAlignment(SWT.RIGHT);
		tcolReserveNo.setWidth(65);
		tcolReserveNo.setText(LangResources.getText("SWTReservationDisplay.tcolReserveNo"));
		
		final TableColumn tcolStartDate = new TableColumn(tblInfo, SWT.NONE);
		tcolStartDate.setWidth(120);
		tcolStartDate.setText(LangResources.getText("SWTReservationDisplay.tcolStartDate"));
		
		final TableColumn tcolEndDate = new TableColumn(tblInfo, SWT.NONE);
		tcolEndDate.setWidth(120);
		tcolEndDate.setText(LangResources.getText("SWTReservationDisplay.tcolEndDate"));
		
		final TableColumn tcolRoom = new TableColumn(tblInfo, SWT.NONE);
		tcolRoom.setWidth(100);
		tcolRoom.setText(LangResources.getText("SWTReservationDisplay.tcolRoom"));
		
		final TableColumn tcolRentType = new TableColumn(tblInfo, SWT.NONE);
		tcolRentType.setWidth(90);
		tcolRentType.setText(LangResources.getText("SWTReservationDisplay.tcolRentType"));
		
		final TableColumn tcolReserveStatus = new TableColumn(tblInfo, SWT.NONE);
		tcolReserveStatus.setWidth(90);
		tcolReserveStatus.setText(LangResources.getText("SWTReservationDisplay.tcolReserveStatus"));
		
		final TableColumn tcolRoomTotal = new TableColumn(tblInfo, SWT.RIGHT);
		tcolRoomTotal.setWidth(90);
		tcolRoomTotal.setText(LangResources.getText("SWTReservationDisplay.tcolRoomTotal"));
		
		final TableColumn tcolOthersTotal = new TableColumn(tblInfo, SWT.RIGHT);
		tcolOthersTotal.setWidth(90);
		tcolOthersTotal.setText(LangResources.getText("SWTReservationDisplay.tcolOthersTotal"));
		
		final TableColumn tcolTotal = new TableColumn(tblInfo, SWT.RIGHT);
		tcolTotal.setWidth(90);
		tcolTotal.setText(LangResources.getText("SWTReservationDisplay.tcolTotal"));
		
		final TableColumn tcolCustomer = new TableColumn(tblInfo, SWT.NONE);
		tcolCustomer.setWidth(120);
		tcolCustomer.setText(LangResources.getText("SWTReservationDisplay.tcolCustomer"));
		
		final Label line3 = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
		line3.setBounds(0, 615, 812, 15);
		line3.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		clblMessage = new CLabel(this, SWT.RIGHT | SWT.SHADOW_IN | SWT.BORDER);
		clblMessage.setBounds(15, 635, 250, 25);
		clblMessage.setForeground(BaseConstant.COLOR_LINK_LABEL);
		clblMessage.setFont(BaseConstant.FONT_BOLD_LABEL);
		clblMessage.setBackground(BaseConstant.COLOR_BGR_LABEL);
		clblMessage.setAlignment(SWT.CENTER);

		btnReserveInput = new Button(this, SWT.NONE);
		btnReserveInput.setBounds(485, 636, 100, 23);
		btnReserveInput.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnReserveInput.setText(LangResources.getText("SWTReservationDisplay.btnReserveInput"));
		btnReserveInput.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnReserveInput.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clblMessage.setText("");
//				saveAction();
			}
		});

		btnClear = new Button(this, SWT.NONE);
		btnClear.setBounds(591, 636, 100, 23);
		btnClear.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClear.setText(LangResources.getText("btnClear"));
		btnClear.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clearAction();
			}
		});

		btnClose = new Button(this, SWT.NONE);
		btnClose.setBounds(697, 636, 100, 23);
		btnClose.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClose.setText(LangResources.getText("btnClose"));
		btnClose.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClose.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (!closeAction()){
					btnClose.setFocus();
				}
			}
		});
		
		this.getShell().setDefaultButton(btnSearch);
		
	}

	/**
	 * clearAction
	 */
	protected void clearAction() {
		
		SWTUtils.OperatorUI.clearAll(this.getChildren());
		SWTUtils.OperatorUI.setEnableAll(grpSearch.getChildren(), true);
		txtRoom.setFocus();
	}

	/**
	 * displayAction
	 */
	protected void searchAction() {
		
//		try {
//			// check action
//			if (!chkInput()){
//				return;
//			}
//			//
//			ReserveForm searchform = new ReserveForm();
//			searchform.setActionForm("reserveForm");
//			searchform.setActionMethod("getReserveInfo");
//			
//			searchform.setRoom(room);
//			
//			searchform = (ReserveForm)execute(searchform);
//			displayTable();
//			
//			tblInfo.setFocus();
//			
//		} catch (SessionTimeOutException e) {
//			log.warn("SessionTimeOutException", e);
//		} catch (ServerException e) {
//			log.error("ServerException.", e);
//			fatalErrorMsgBox();
//		} catch (FatalException e) {
//			log.error("FatalException.", e);
//			fatalErrorMsgBox();
//		}
	}

	/**
	 * display table program
	 * @param list
	 */
//	private void displayTable(ArrayList list) {
//
//		tblInfo.removeAll();
//		
//		int rowcount = TBL_ROW_COUNT;
//		if (list.size() >= rowcount){
//			rowcount = list.size() + 2;
//		}
//		for (int i = 0; i < rowcount; i++) {
//			TableItem tblItem = new TableItem(tblInfo, SWT.NONE);
//			
//			if (i < list.size()){
//				UserDTO user = (UserDTO)list.get(i);
//				
//				String[] items = new String[tblInfo.getColumnCount()];
//				int idx = 0;
//				items[idx++] = "";
//				items[idx++] = Integer.toString(user.getUserId());
//				items[idx++] = user.getUserCd();
//				items[idx++] = user.getName();
//				items[idx++] = user.getUserName();
//				items[idx++] = user.getPassword();
//				items[idx++] = user.getLockoutDate();
//				items[idx++] = user.getMail();
//				
//				tblItem.setText(items);
//				tblItem.setData(user);
//			}
//			
//			if (i%2 == 0){
//				tblItem.setBackground(BaseConstant.COLOR_TABLE_ROW_EVEN);
//			}
//		}
//		tblInfo.setData(list);
//		tblInfo.setSelection(0);
//	}


	/**
	 * check data input
	 * @return boolean
	 */
//	private boolean chkInput() {
//		
//		try {
//			if (!SWTUtils.Check.checkRequired(txtUsercd, 
//					LangResources.getText("SWTReservationDisplay.lblUserCd"))){
//				return false;
//			}
//			
//			//check only Latin AlphaB, and number
//			if (!SWTUtils.Check.checkAlphaNummericOnly(txtUsercd, 
//					LangResources.getText("SWTReservationDisplay.lblUserCd"))){
//				return false;
//			}
//			
//			if (!SWTUtils.Check.checkRequired(txtName, 
//					LangResources.getText("SWTReservationDisplay.lblName"))){
//				return false;
//			}
//			
//			if (!SWTUtils.Check.checkRequired(txtAccount, 
//					LangResources.getText("SWTReservationDisplay.lblAccount"))){
//				return false;
//			}
//			
//			if (!SWTUtils.Check.checkRequired(txtPassword, 
//					LangResources.getText("SWTReservationDisplay.lblPassword"))){
//				return false;
//			}
//			
//			if (!Utils.isNullEmpty(txtEmail.getText())){
//				if (!SWTUtils.Check.checkEmail(txtEmail, 
//						LangResources.getText("SWTReservationDisplay.lblEmail"))){
//					return false;
//				}
//			}
//			
//			if (chkDetail.getSelection()){
//				if (!SWTUtils.Check.checkRequired(txtIDNumber, 
//						LangResources.getText("SWTReservationDisplay.lblIDNumber"))){
//					return false;
//				}
//				
//				//check only Latin AlphaB, and number
//				if (!SWTUtils.Check.checkNumeric(txtIDNumber, 
//						LangResources.getText("SWTReservationDisplay.lblIDNumber"))){
//					return false;
//				}
//				
//				if (!Utils.isNullEmpty(txtBirthday.getText())){
//					//check format YYYY/MM/DD
//					if (!SWTUtils.Check.checkYYYYMMDDFormat(txtBirthday.getText(), 
//							LangResources.getText("SWTReservationDisplay.lblBirthday"))){
//						txtBirthday.setFocus();
//						return false;
//					}
//				}
//			}
//			
//		} catch (ClientException e) {
//			log.error("ClientException.", e);
//			fatalErrorMsgBox();
//			return false;
//		}
//		return true;
//	}

	/**
	 * lockScreen
	 * @param flg 
	 */
	protected void lockScreen(boolean flg) {
		
		SWTUtils.OperatorUI.setEnableAll(this.getChildren(), !flg);
	}

	@Override
	protected boolean loadData() throws FatalException {
		// TODO Auto-generated method stub
		return false;
	}
}